package v1

import (
	"net"
	"net/http"
)

type HandleFunc func(ctx Context)

type HttpServer struct {
	router *Router
}

type Server interface {
	http.Handler

	Start(addr string) error
}

type Person struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func (h *HttpServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	ctx := Context{
		Req:  req,
		Resp: resp,
	}

	key := req.Method + "_" + req.URL.Path
	handle := h.router.get(key)
	handle.handler(ctx)

}

func (h *HttpServer) Start(addr string) error {
	l, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}
	return http.Serve(l, h)
}
